package com.ebspp.server;

import org.apache.thrift.server.TServer;
import org.apache.thrift.server.TThreadPoolServer;
import org.apache.thrift.transport.TServerSocket;
import org.apache.thrift.transport.TTransportException;

import com.ebspp.global.General;
import com.ebspp.thrift.DataWrite;

/**
 * a EBS++ server responsible for data connection
 * Note that all write to it should be replicated. either asynchronously by default, or
 * synchronously if indicated by provision call
 * The replication functionality is under construction.
 * 
 * @author x23pan
 *
 */
public class EBSPPWriteServer extends EBSPPServer
{
	
	public static void initServer()
	{	 
		try 
		{
			TServerSocket serverTransport = new TServerSocket(General.wPort);
			
			DataWrite.Processor processor = new DataWrite.Processor(new WriteHandler());

			TServer server = new TThreadPoolServer(
								new TThreadPoolServer.Args(serverTransport).processor(processor));
			/*
			TServer server = new TThreadPoolServer(
					new TThreadPoolServer.Args(serverTransport).processor(processor).
					protocolFactory(new TBinaryProtocol.Factory(true, true)));*/
			
			System.out.println("Start write server at port: " + General.wPort);
			
			server.serve();
			System.err.println("Write server closed");
		} catch (TTransportException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
/*
		try 
		{
			TNonblockingServerTransport serverTransport = new TNonblockingServerSocket(port);
			DataWrite.Processor processor = new DataWrite.Processor(new WriteHandler());
			TServer server = new TNonblockingServer(
					new TNonblockingServer.Args(serverTransport).processor(processor));
			
			System.out.println("Staring data server at port: " + port);
			server.serve();
			
		} catch (TTransportException e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}*/
		
	}
}
